if not pattern then pattern = {} end
if not pattern.ticks then pattern.ticks = {} end

if not pattern.ticks.markets then

pattern.ticks.markets = {}
pattern.ticks.markets.trends = {}

dofile (ScriptsRootPath .. "lib\\ticks.lua")

function pattern.ticks.markets.Init()
	pattern.ticks.markets.params = lib:LoadIni(ticks.patterns_init, "markets", {shift = 0.1, trend = 0.5, period = 30})
	if not ticks.clean_period
		or ticks.clean_period < pattern.ticks.markets.params.period * 60
	then
		ticks.clean_period = pattern.ticks.markets.params.period * 60
	end
	if processor and processor.log then
		processor:log("pattern.ticks.markets.params", pattern.ticks.markets.params)
	end
end

function pattern.ticks.markets.OnTick(tick)
	if not tick then return end
	if not pattern.ticks.markets.params then pattern.ticks.markets.Init() end
	
	local price, datetime
	local ct, m = ticks:get(tick)
	if ct and tick.time ~= ct.time and #m > 5 then
		local side = 0
		for i, t in ipairs(m) do
			if t.side then
				side = side + 1
			else
				side = side - 1
			end
		end
		if side ~= 0 then
			local pmax, pmin
			for i, t in ipairs(m) do
				if t.price and (t.side and side > 0 or not t.side and side < 0) then
					if not pmin or pmin > t.price then
						pmin = t.price
					end
					if not pmax or pmax < t.price then
						pmax = t.price
					end
				end
			end
			if pmax and pmin and side ~= 0 then
				local sh = round((pmax - pmin) * 100 / pmin, 2)
				if sh >= pattern.ticks.markets.params.shift then
					datetime = lib:tickdate(tick.time)
					datetime.is_long__ = side > 0
					if datetime.is_long__ then
						price = pmax
					else
						price = pmin
					end
							
					
					if not pattern.ticks.markets.trends[tick.sec_code] then pattern.ticks.markets.trends[tick.sec_code] = {} end
					if not Settings then
						ticks:GetTrend(tick, pattern.ticks.markets.trends[tick.sec_code], pattern.ticks.markets.params.period)
			
						if datetime.is_long__ then		-- buy order
							local hint = string.format("MARKET %s BID %s %s %s%%",
								lib:FormatPrice(sh),
								lib:FormatPrice(price),
								lib:bool2side(pattern.ticks.markets.trends[tick.sec_code].is_long),
								tostring(pattern.ticks.markets.trends[tick.sec_code].percent)
							)
							local notif = tick.sec_code .. " " .. hint
							if processor and processor.log then
								processor:log(notif .. " " .. lib:toline(pattern.ticks.markets.trends[tick.sec_code]))
							end
		
							if pattern.ticks.markets.trends[tick.sec_code].percent
								and (pattern.ticks.markets.trends[tick.sec_code].percent < pattern.ticks.markets.params.trend
									or pattern.ticks.markets.trends[tick.sec_code].pmax - tick.price
									> tick.price - pattern.ticks.markets.trends[tick.sec_code].pmin
								)
							then return end
							
							datetime.sec_code__ = tick.sec_code
							datetime.labels__ = {
								lm = pmax,
								sl = pattern.ticks.markets.trends[tick.sec_code].pmax or pmax * 1.0025,
								tp = pattern.ticks.markets.trends[tick.sec_code].pmin or pmax * 0.9925
							}
							
							--datetime.price__ = pmax
							datetime.hint__ = hint
							datetime.notify__ = notif
						else		-- sell order
							local hint = string.format("MARKET %s OFFER %s %s %s%%",
								lib:FormatPrice(sh),
								lib:FormatPrice(price),
								lib:bool2side(pattern.ticks.markets.trends[tick.sec_code].is_long),
								tostring(pattern.ticks.markets.trends[tick.sec_code].percent)
							)
							local notif = tick.sec_code .. " " .. hint
							if processor and processor.log then
								processor:log(notif .. " " .. lib:toline(pattern.ticks.markets.trends[tick.sec_code]))
							end
		
							if pattern.ticks.markets.trends[tick.sec_code].percent
								and (pattern.ticks.markets.trends[tick.sec_code].percent < pattern.ticks.markets.params.trend
									or pattern.ticks.markets.trends[tick.sec_code].pmax - tick.price
									< tick.price - pattern.ticks.markets.trends[tick.sec_code].pmin
								)
							then return end
							
							datetime.sec_code__ = tick.sec_code
							datetime.labels__ = {
								lm = pmin,
								sl = pattern.ticks.markets.trends[tick.sec_code].pmin or pmin * 0.9975,
								tp = pattern.ticks.markets.trends[tick.sec_code].pmax or pmin * 1.0075
							}
							
							--datetime.price__ = pmin
							datetime.hint__ = hint
							datetime.notify__ = notif
						end
					end
					
					return price, datetime
				end
			end
		end
	end
end

end -- of file


